package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iretailer.report.model.FaceAlarmDevice;
import com.iretailer.report.model.FaceAlarmMall;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.FaceAlarmMapper;
import com.iretailer.report.model.FaceAlarm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/11/14 0014 11:33
 * @Description:
 */
@Service
public class FaceAlarmService {

    @Autowired
    private FaceAlarmMapper faceAlarmMapper;
    @Autowired
    private UserMallService userMallService;


    public PageResult<FaceAlarmMall> gridFaceAlarm(String userId, String mallName, String name, QueryOption queryOption) {
        Page<FaceAlarmMall> page = new Page<>(1, Long.MAX_VALUE);
        QueryWrapper<FaceAlarmMall> queryWrapper = new QueryWrapper<>();
        //这里设置一个 1 =1 ，当下面条件都没有的时候 不会生成一个 where
        queryWrapper.eq("1", 1);
        if (mallName != null) {
            queryWrapper.like("mallName", mallName);
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("mallId").orderByAsc("Name");
        if (userId != null) {
            List<String> mallIds = userMallService.selectUserMallIds(userId);
            queryWrapper.in("mallId", mallIds);
        }
        faceAlarmMapper.selectPageFaceAlarmMall(page, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @CacheEvict(value = "getFaceAlarmDevice", allEntries = true)
    public boolean saveOrUpdateFaceAlarm(FaceAlarm faceAlarm) {
        if (faceAlarm.getId() != null && faceAlarm.getId() > 0) {
            faceAlarmMapper.updateById(faceAlarm);
        } else {
            faceAlarmMapper.insert(faceAlarm);
        }
        return true;
    }

    @CacheEvict(value = "getFaceAlarmDevice", allEntries = true)
    public boolean deleteFaceAlarmByIds(List<Long> ids) {
        List<Long> dids = faceAlarmMapper.selectDidsByAlarmIds(ids);
        if (!dids.isEmpty()) {
            return false;
        } else {
            faceAlarmMapper.deleteBatchIds(ids);
            return true;
        }
    }

    @CacheEvict(value = "getFaceAlarmDevice", allEntries = true)
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean saveFaceAlarmDevice(Long id, List<Long> deviceIds) {
        if (id != null && id > 0 && deviceIds != null) {
            faceAlarmMapper.deleteFaceAlarmDeviceByFaceAlarmId(id);
            if (deviceIds.size() > 0) {
                faceAlarmMapper.batchInsertFaceAlarmDevice(id, deviceIds);
            }
            return true;
        } else {
            return false;
        }
    }

    public List<Long> selectDidsByFaceAlarmId(Long id) {
        return faceAlarmMapper.selectDidsByAlarmIds(Arrays.asList(id));
    }
    @Cacheable(value = "getFaceAlarmDevice", key = "#deviceId")
    public List<FaceAlarmDevice> getFaceAlarmDeviceByDeviceId(String deviceId){
        return faceAlarmMapper.getFaceAlarmDeviceByDeviceId(deviceId);
    }
}
